package com.chaintor.demo.math;

import org.apache.commons.math3.util.FastMath;

import java.util.Vector;

/**
 * 测试插方进化类
 */
public class Rastrigin implements DiffEvolutionOptimizable {

    public Rastrigin(){
        m_dim = 5;
    }

    public Rastrigin(int dims){
        m_dim = dims;
    }

    int m_dim;

    public double evaluteCost(double[] inputs) {
        if (inputs.length != m_dim){
            System.err.println(new Exception("参数数量错误！"));
        }

        double A = 10;

        double val = 0.0;

        for (int i = 0; i < m_dim; i++)
        {
            if (inputs[i] < -5.12 || inputs[i] > 5.12)
            {
                return 1e7;
            }

            val += inputs[i] * inputs[i] - A * FastMath.cos(2 * FastMath.PI * inputs[i]);
        }

        return A * m_dim + val;

    }

    public int numberOfParameters() {
        return m_dim;
    }

    public Vector<Constraints> GetConstraints() {
        Vector<Constraints> constr = new Vector<Constraints>(m_dim);
        for (int i=0;i<m_dim;i++) {
            Constraints c = new Constraints(-5.12,5.12,true);
            constr.addElement(c);
        }
        return constr;
    }

    public double getAlpha() {
        return 0;
    }
}
